package org.mypro.front;

import java.util.Date;
import java.util.List;


import org.mypro.dao.KdyMapper;
import org.mypro.dao.KhMapper;
import org.mypro.dao.PdyMapper;
import org.mypro.dao.UserMapper;

import org.mypro.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.mypro.util.PageUtil;

/**

	


**/

@Controller
@RequestMapping(value = "user")
public class UserController{
	private static final long serialVersionUID = -9167213418951271794L;

	private int pageSize = 10;
	private int currentPage = 1;
	

	

	@Autowired
	private UserMapper entityDao;

	@Autowired
	private PdyMapper pdyDao;

	@Autowired
	private KdyMapper kdyDao;

	@Autowired
	private KhMapper khDao;


	@RequestMapping(value = "list")
	public ModelAndView list(User entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/list");
		try {
			int listCount = entityDao.countList(entity);
			PageUtil pageUtil = new PageUtil();
			if (listCount > 0) {
				pageUtil.setPageSize(pageSize);// 每页显示的记录数量
				pageUtil.setTotalRecord(listCount);// 总记录数
				pageUtil.setCurrentPage(currentPage);// 当前页码
				entity.setBeginRow(pageUtil.getStart());
				entity.setPageSize(pageUtil.getPageSize());
				List<User> entityList = (List<User>)entityDao.findList(entity);
				mv.addObject("list", entityList);
			} else {
				pageUtil.setPageSize(pageSize);// 每页显示的记录数量
				pageUtil.setTotalRecord(0);// 总记录数
				pageUtil.setCurrentPage(currentPage);// 当前页码
			}
			mv.addObject("message", "查询成功！");
			mv.addObject("status", true);
			mv.addObject("pageUtil", pageUtil);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "查询失败！");
			mv.addObject("status", "查询失败！");
			mv.addObject("status", false);
			mv.addObject("status", false);
		}
		return mv;
	}

	
	@RequestMapping(value = "query")
	public ModelAndView query(User entity){
	
	
	ModelAndView mv =list(entity);
		mv.setViewName("houtai/user/query");
		return mv;
	}
	
	@RequestMapping(value = "listAll")
	public ModelAndView listAll(User entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/listAll");
		try {
			List<User> entityList = (List<User>)entityDao.findAllList(entity);
			mv.addObject("list", entityList);
			mv.addObject("message", "查询成功！");
			mv.addObject("status", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "查询失败！");
			mv.addObject("status", false);
		}
		return mv;
	}
	
	@RequestMapping(value = "edit")
	public ModelAndView edit(User entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/edit");
		try {
			mv.addObject("entity", entityDao.selectByPrimaryKey(entity.getId()));
			
			mv.addObject("message", "查询成功！");
			mv.addObject("status", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "查询失败！");
			mv.addObject("status", false);
		}
		return mv;
	}

	@RequestMapping(value = "add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/edit");
		
		return mv;
	}
	@RequestMapping(value = "addkdy", method = RequestMethod.GET)
	public ModelAndView addkdy(){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/addkdy");
		return mv;
	}
	
	@RequestMapping(value = "addkdy", method = RequestMethod.POST)
	public ModelAndView addkdy(User entity){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/addkdy");
		try {
			entity.setCreatetime(new Date());
			entityDao.insert(entity);
			mv.addObject("message", "添加成功！");
			mv.addObject("status_", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "修改失败！");
			mv.addObject("status_", false);
		}
		return mv;
	}


	@RequestMapping(value = "saveUpdate", method = RequestMethod.POST)
	public ModelAndView saveUpdate(User entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/user/list.action");
		try {
			entity.setCreatetime(new Date());
			if(entity.getId()!=null){
				entityDao.updateByPrimaryKey(entity);
			}else{
				entityDao.insert(entity);
			}
			mv.addObject("message", "修改成功！");
			mv.addObject("status", true);
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "修改失败！");
			mv.addObject("status", false);
		}
		return mv;
	}
	@RequestMapping(value = "update" , method = RequestMethod.GET)
	public ModelAndView update(String id){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/update");
		mv.addObject("entity",entityDao.selectByPrimaryKey(Integer.parseInt(id)));
		return mv;
	}
	
	@RequestMapping(value = "update" , method = RequestMethod.POST)
	public ModelAndView update(User user){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("houtai/user/update");
		entityDao.updateByPrimaryKey(user);
		mv.addObject("message", "修改成功！");
		return mv;
	}
	
	@RequestMapping(value = "delete")
	public ModelAndView delete(User entity) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/user/list.action");
		try {
			if(entityDao.deleteByPrimaryKey(entity.getId())>0){
				mv.addObject("message", "删除成功！");
				mv.addObject("status", true);
			}else{
				mv.addObject("message", "删除失败！");
				mv.addObject("status", false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("message", "删除失败！");
			mv.addObject("status", false);
		}
		return mv;
	}


}
